import DrawV2 from "../../../engine/comps/DrawV2";
import MathHelper from "../../../cfw/tools/MathHelper";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { EventName } from "../../../public/Config";
import CookeryRoomStep from "./CookeryRoomStep";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CookeryRoomStep0 extends CookeryRoomStep {

    @property(cc.Node)
    handle: cc.Node = null;

    @property(DrawV2)
    draw: DrawV2 = null;

    @property(cc.Node)
    bing: cc.Node = null;

    @property(cc.Node)
    lump: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    protected pointList: cc.Vec2[] = []

    start() {
        this.node.active = true;
        let x = this.bing.x;
        let y = this.bing.y;
        let w = this.bing.width / 4
        let h = this.bing.height / 4
        this.pointList.push(cc.v2(x, y))
        this.push(x, y, w, h)
        // this.push(x, y, w / 2, h / 2)
        this.handle.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)
        this.handle.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this)
        this.handle.on(cc.Node.EventType.TOUCH_END, this.touchCancel, this)
    }

    push(x, y, w, h) {

        this.pointList.push(cc.v2(x - w, y))
        this.pointList.push(cc.v2(x + w, y))
        this.pointList.push(cc.v2(x, y + h))
        this.pointList.push(cc.v2(x - w, y + h))
        this.pointList.push(cc.v2(x + w, y + h))
        this.pointList.push(cc.v2(x, y - h))
        this.pointList.push(cc.v2(x - w, y - h))
        this.pointList.push(cc.v2(x + w, y - h))
    }

    touchCancel() {


    }

    touchStart(e) {
        this.painting(e)
    }

    touchMove(e: cc.Touch) {
        this.painting(e)
    }
    
    painting(e) {
        if (this.pointList.length <= 0) {
            return;
        }
        let pos = e.getLocation();

        let wpos = this.node.convertToNodeSpaceAR(pos)
        // cc.log(' wpos  ', wpos.x, wpos.y)
        this.handle.x = wpos.x
        this.handle.y = wpos.y;
        this.draw.paintStart(this.draw.node.convertToNodeSpaceAR(pos))
        for (let index = 0; index < this.pointList.length; index++) {
            const element = this.pointList[index];
            // cc.log(' elment  ', element.x, element.y)
            if (MathHelper.distance(element.x, element.y, wpos.x, wpos.y) <= 80) {
                this.pointList.splice(index, 1)
                break;
            }
        }
        if (this.pointList.length <= 0) {
            cc.log(' 擀面完成=== ')
            this.draw.node.opacity = 0;
            this.bing.active = true;
            this.lump.opacity = 0;
            this.bing.opacity = 0;
            cc.tween(this.bing).to(1, { opacity: 255 }).call(this.finish.bind(this)).start()
            cc.tween(this.handle).by(0.3, { x: -400, y: -1000 }).start();
        }
    }

    finish() {
        cc.log(' finish == ')
         GlobalEvent.instance().emit(EventName.UPDATE_COOKIE_STEP, 0)

    }


    // update (dt) {}
}
